<!DOCTYPE html>
<html>
  
<head>
  <meta charset="utf-8">
  <meta name="author" content="饿包子" />
  
  
  <title>【转载】你会用ES6，那倒是用啊！ | 饿包子博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="文章转载," />
  

  
  <meta name="description" content="该文章为转载文章，因为很多时候看到一篇文章，但过段时间就可能会被删除，所以先转载过来，怕不知道什么时候就没了或找不到。作者要是联系，我也会删除。文章不代表本人观点">

  
  
    <link rel="icon" href="/blog/images/xian.ico">
    <link rel="apple-touch-icon" href="/blog/images/Logo.png">
  

  
<link rel="stylesheet" href="/blog/css/Awesome.min.css">


  
<link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">


  
  
<script src="/blog/common/jquery.min.js"></script>

  
    
<script src="/blog/common/Av.min.js"></script>

  

  
    
<script src="/blog/common/MathJax.js"></script>

  

  
    
<script src="/blog/common/Valine.min.js"></script>

  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"gE1EosDsVgB2slV4E92yPkcG-gzGzoHsz","appkey":"niR787UVTMyJQrR6gmOiJCKe","comment":true,"count":true},
    welcome: {"enable":false,"interval":30},
    start_time: "2018-06-10",
    passwords: ["a621ab606db2a11f63edc576a729843b8269250dc324206871d90635ac5e531c", ],
    is_post: true,
    lock: false,
    author: "饿包子",
    share: {"twitter":false,"facebook":false,"weibo":true,"qq":true,"wechat":false},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  
<script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">清风逐月</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 我思故我在</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">主页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">日记</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://github.com/lixianbin1/" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            主页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">
            日记
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2022-03-01
    </span>
    
      <span>
        | <a href="/blog/categories/%E6%96%87%E7%AB%A0%E8%BD%AC%E8%BD%BD/"><i class="fa fa-bookmark"></i>文章转载</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
    <span>
       | <i class="fa fa-calendar"></i>更新时间:2022-3-1 9:31 
    </span>
  </div>
  <h1 class="passage-title">
    【转载】你会用ES6，那倒是用啊！
  </h1>
  
  <article class="passage-article">
    <p>该文章为转载文章，因为很多时候看到一篇文章，但过段时间就可能会被删除，所以先转载过来，怕不知道什么时候就没了或找不到。作者要是联系，我也会删除。文章不代表本人观点</p>
<p>【转载】：<a target="_blank" rel="noopener" href="https://juejin.cn/post/7016520448204603423">https://juejin.cn/post/7016520448204603423</a></p>
<p>不是标题党，这是一位leader在一次代码评审会对小组成员发出的“怒吼”，原因是在代码评审中发现很多地方还是采用ES5的写法，也不是说用ES5写法不行，会有BUG，只是造成代码量增多，可读性变差而已。<br>恰好，这位leader有代码洁癖，面对3~5年经验的成员，还写这种水平的代码，极为不满，不断对代码进行吐槽。不过对于他的吐槽，我感觉还是有很大收获的，故就把leader的吐槽记录下来，分享给掘友们，觉得有收获点个赞，有错误的或者更好的写法，非常欢迎在评论中留言。</p>
<p>ps：ES5之后的JS语法统称ES6！！！</p>
<h3 id="一、关于取值的吐槽"><a href="#一、关于取值的吐槽" class="headerlink" title="一、关于取值的吐槽"></a>一、关于取值的吐槽</h3><p>取值在程序中非常常见，比如从对象obj中取值</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const obj = &#123;</span><br><span class="line">    a:1,</span><br><span class="line">    b:2,</span><br><span class="line">    c:3,</span><br><span class="line">    d:4,</span><br><span class="line">    e:5,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>吐槽：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const a = obj.a;</span><br><span class="line">const b = obj.b;</span><br><span class="line">const c = obj.c;</span><br><span class="line">const d = obj.d;</span><br><span class="line">const e = obj.e;</span><br></pre></td></tr></table></figure>

<p>或者</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const f = obj.a + obj.d;</span><br><span class="line">const g = obj.c + obj.e;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>吐槽：“不会用ES6的解构赋值来取值吗？5行代码用1行代码搞定不香吗？直接用对象名加属性名去取值，要是对象名短还好，很长呢？搞得代码中到处都是这个对象名。”</p>
</blockquote>
<p>改进：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">const &#123;a,b,c,d,e&#125; = obj;</span><br><span class="line">const f = a + d;</span><br><span class="line">const g = c + e;</span><br></pre></td></tr></table></figure>

<p>反驳<br>不是不用ES6的解构赋值，而是服务端返回的数据对象中的属性名不是我想要的，这样取值，不是还得重新创建个遍历赋值。</p>
<p>吐槽<br>看来你对ES6的解构赋值掌握的还是不够彻底。如果想创建的变量名和对象的属性名不一致，可以这么写：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const &#123;a:a1&#125; = obj;</span><br><span class="line">console.log(a1);// 1</span><br></pre></td></tr></table></figure>

<p>补充<br>ES6的解构赋值虽然好用。但是要注意解构的对象不能为undefined、null。否则会报错，故要给被解构的对象一个默认值。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const &#123;a,b,c,d,e&#125; = obj || &#123;&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="二、关于合并数据的吐槽"><a href="#二、关于合并数据的吐槽" class="headerlink" title="二、关于合并数据的吐槽"></a>二、关于合并数据的吐槽</h3><p>比如合并两个数组，合并两个对象。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">const a = [1,2,3];</span><br><span class="line">const b = [1,5,6];</span><br><span class="line">const c = a.concat(b);//[1,2,3,1,5,6]</span><br><span class="line"></span><br><span class="line">const obj1 = &#123;</span><br><span class="line">  a:1,</span><br><span class="line">&#125;</span><br><span class="line">const obj2 = &#123;</span><br><span class="line">  b:1,</span><br><span class="line">&#125;</span><br><span class="line">const obj = Object.assign(&#123;&#125;, obj1, obj2);//&#123;a:1,b:1&#125;</span><br></pre></td></tr></table></figure>

<p>吐槽<br>ES6的扩展运算符是不是忘记了，还有数组的合并不考虑去重吗？</p>
<p>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">const a = [1,2,3];</span><br><span class="line">const b = [1,5,6];</span><br><span class="line">const c = [...new Set([...a,...b])];//[1,2,3,5,6]</span><br><span class="line"></span><br><span class="line">const obj1 = &#123;</span><br><span class="line">  a:1,</span><br><span class="line">&#125;</span><br><span class="line">const obj2 = &#123;</span><br><span class="line">  b:1,</span><br><span class="line">&#125;</span><br><span class="line">const obj = &#123;...obj1,...obj2&#125;;//&#123;a:1,b:1&#125;</span><br></pre></td></tr></table></figure>
<h3 id="三、关于拼接字符串的吐槽"><a href="#三、关于拼接字符串的吐槽" class="headerlink" title="三、关于拼接字符串的吐槽"></a>三、关于拼接字符串的吐槽</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">const name = &#x27;小明&#x27;;</span><br><span class="line">const score = 59;</span><br><span class="line">let result = &#x27;&#x27;;</span><br><span class="line">if(score &gt; 60)&#123;</span><br><span class="line">  result = `$&#123;name&#125;的考试成绩及格`; </span><br><span class="line">&#125;else&#123;</span><br><span class="line">  result = `$&#123;name&#125;的考试成绩不及格`; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>吐槽<br>像你们这样用ES6字符串模板，还不如不用，你们根本不清楚在${}中可以做什么操作。在${}中可以放入任意的JavaScript表达式，可以进行运算，以及引用对象属性。<br>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">const name = &#x27;小明&#x27;;</span><br><span class="line">const score = 59;</span><br><span class="line">const result = `$&#123;name&#125;$&#123;score &gt; 60?&#x27;的考试成绩及格&#x27;:&#x27;的考试成绩不及格&#x27;&#125;`;</span><br></pre></td></tr></table></figure>
<h3 id="四、关于if中判断条件的吐槽"><a href="#四、关于if中判断条件的吐槽" class="headerlink" title="四、关于if中判断条件的吐槽"></a>四、关于if中判断条件的吐槽</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">if(</span><br><span class="line">    type == 1 ||</span><br><span class="line">    type == 2 ||</span><br><span class="line">    type == 3 ||</span><br><span class="line">    type == 4 ||</span><br><span class="line">)&#123;</span><br><span class="line">   //...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>吐槽<br>ES6中数组实例方法includes会不会使用呢？<br>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const condition = [1,2,3,4];</span><br><span class="line"></span><br><span class="line">if( condition.includes(type) )&#123;</span><br><span class="line">   //...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="五、关于列表搜索的吐槽"><a href="#五、关于列表搜索的吐槽" class="headerlink" title="五、关于列表搜索的吐槽"></a>五、关于列表搜索的吐槽</h3><p>在项目中，一些没分页的列表的搜索功能由前端来实现，搜索一般分为精确搜索和模糊搜索。搜索也要叫过滤，一般用filter来实现。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const a = [1,2,3,4,5];</span><br><span class="line">const result = a.filter( </span><br><span class="line">  item =&gt;&#123;</span><br><span class="line">    return item === 3</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<p>吐槽<br>如果是精确搜索不会用ES6中的find吗？性能优化懂么，find方法中找到符合条件的项，就不会继续遍历数组。<br>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const a = [1,2,3,4,5];</span><br><span class="line">const result = a.find( </span><br><span class="line">  item =&gt;&#123;</span><br><span class="line">    return item === 3</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<h3 id="六、关于扁平化数组的吐槽"><a href="#六、关于扁平化数组的吐槽" class="headerlink" title="六、关于扁平化数组的吐槽"></a>六、关于扁平化数组的吐槽</h3><p>一个部门JSON数据中，属性名是部门id，属性值是个部门成员id数组集合，现在要把有部门的成员id都提取到一个数组集合中。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">const deps = &#123;</span><br><span class="line">&#x27;采购部&#x27;:[1,2,3],</span><br><span class="line">&#x27;人事部&#x27;:[5,8,12],</span><br><span class="line">&#x27;行政部&#x27;:[5,14,79],</span><br><span class="line">&#x27;运输部&#x27;:[3,64,105],</span><br><span class="line">&#125;</span><br><span class="line">let member = [];</span><br><span class="line">for (let item in deps)&#123;</span><br><span class="line">    const value = deps[item];</span><br><span class="line">    if(Array.isArray(value))&#123;</span><br><span class="line">        member = [...member,...value]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">member = [...new Set(member)]</span><br></pre></td></tr></table></figure>
<p>吐槽<br>获取对象的全部属性值还要遍历吗？Object.values忘记了吗？还有涉及到数组的扁平化处理，为啥不用ES6提供的flat方法呢，还好这次的数组的深度最多只到2维，还要是遇到4维、5维深度的数组，是不是得循环嵌套循环来扁平化？<br>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">const deps = &#123;</span><br><span class="line">    &#x27;采购部&#x27;:[1,2,3],</span><br><span class="line">    &#x27;人事部&#x27;:[5,8,12],</span><br><span class="line">    &#x27;行政部&#x27;:[5,14,79],</span><br><span class="line">    &#x27;运输部&#x27;:[3,64,105],</span><br><span class="line">&#125;</span><br><span class="line">let member = Object.values(deps).flat(Infinity);</span><br></pre></td></tr></table></figure>
<p>其中使用Infinity作为flat的参数，使得无需知道被扁平化的数组的维度。<br>补充<br>flat方法不支持IE浏览器。</p>
<h3 id="七、关于获取对象属性值的吐槽"><a href="#七、关于获取对象属性值的吐槽" class="headerlink" title="七、关于获取对象属性值的吐槽"></a>七、关于获取对象属性值的吐槽</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const name = obj &amp;&amp; obj.name;</span><br></pre></td></tr></table></figure>
<p>吐槽<br>ES6中的可选链操作符会使用么？<br>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const name = obj?.name;</span><br></pre></td></tr></table></figure>
<h3 id="八、关于添加对象属性的吐槽"><a href="#八、关于添加对象属性的吐槽" class="headerlink" title="八、关于添加对象属性的吐槽"></a>八、关于添加对象属性的吐槽</h3><p>当给对象添加属性时，如果属性名是动态变化的，该怎么处理。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">let obj = &#123;&#125;;</span><br><span class="line">let index = 1;</span><br><span class="line">let key = `topic$&#123;index&#125;`;</span><br><span class="line">obj[key] = &#x27;话题内容&#x27;;</span><br></pre></td></tr></table></figure>
<p>吐槽<br>为何要额外创建一个变量。不知道ES6中的对象属性名是可以用表达式吗？<br>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">let obj = &#123;&#125;;</span><br><span class="line">let index = 1;</span><br><span class="line">obj[`topic$&#123;index&#125;`] = &#x27;话题内容&#x27;;</span><br></pre></td></tr></table></figure>
<p>复制代码</p>
<h3 id="九、关于输入框非空的判断"><a href="#九、关于输入框非空的判断" class="headerlink" title="九、关于输入框非空的判断"></a>九、关于输入框非空的判断</h3><p>在处理输入框相关业务时，往往会判断输入框未输入值的场景。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">if(value !== null &amp;&amp; value !== undefined &amp;&amp; value !== &#x27;&#x27;)&#123;</span><br><span class="line">    //...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>吐槽<br>ES6中新出的空值合并运算符了解过吗，要写那么多条件吗？</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">if((value??&#x27;&#x27;) !== &#x27;&#x27;)&#123;</span><br><span class="line">  //...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="十、关于异步函数的吐槽"><a href="#十、关于异步函数的吐槽" class="headerlink" title="十、关于异步函数的吐槽"></a>十、关于异步函数的吐槽</h3><p>异步函数很常见，经常是用 Promise 来实现。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">const fn1 = () =&gt;&#123;</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    setTimeout(() =&gt; &#123;</span><br><span class="line">      resolve(1);</span><br><span class="line">    &#125;, 300);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line">const fn2 = () =&gt;&#123;</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    setTimeout(() =&gt; &#123;</span><br><span class="line">      resolve(2);</span><br><span class="line">    &#125;, 600);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br><span class="line">const fn = () =&gt;&#123;</span><br><span class="line">   fn1().then(res1 =&gt;&#123;</span><br><span class="line">      console.log(res1);// 1</span><br><span class="line">      fn2().then(res2 =&gt;&#123;</span><br><span class="line">        console.log(res2)</span><br><span class="line">      &#125;)</span><br><span class="line">   &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>吐槽<br>如果这样调用异步函数，不怕形成地狱回调啊！<br>改进</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const fn = async () =&gt;&#123;</span><br><span class="line">  const res1 = await fn1();</span><br><span class="line">  const res2 = await fn2();</span><br><span class="line">  console.log(res1);// 1</span><br><span class="line">  console.log(res2);// 2</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>补充<br>但是要做并发请求时，还是要用到Promise.all()。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const fn = () =&gt;&#123;</span><br><span class="line">   Promise.all([fn1(),fn2()]).then(res =&gt;&#123;</span><br><span class="line">       console.log(res);// [1,2]</span><br><span class="line">   &#125;) </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果并发请求时，只要其中一个异步函数处理完成，就返回结果，要用到Promise.race()。</p>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%85%B3%E4%BA%8E%E5%8F%96%E5%80%BC%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">一、关于取值的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%85%B3%E4%BA%8E%E5%90%88%E5%B9%B6%E6%95%B0%E6%8D%AE%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">二、关于合并数据的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%85%B3%E4%BA%8E%E6%8B%BC%E6%8E%A5%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">三、关于拼接字符串的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B%E3%80%81%E5%85%B3%E4%BA%8Eif%E4%B8%AD%E5%88%A4%E6%96%AD%E6%9D%A1%E4%BB%B6%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">四、关于if中判断条件的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%94%E3%80%81%E5%85%B3%E4%BA%8E%E5%88%97%E8%A1%A8%E6%90%9C%E7%B4%A2%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">五、关于列表搜索的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AD%E3%80%81%E5%85%B3%E4%BA%8E%E6%89%81%E5%B9%B3%E5%8C%96%E6%95%B0%E7%BB%84%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">六、关于扁平化数组的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%83%E3%80%81%E5%85%B3%E4%BA%8E%E8%8E%B7%E5%8F%96%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E5%80%BC%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">七、关于获取对象属性值的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AB%E3%80%81%E5%85%B3%E4%BA%8E%E6%B7%BB%E5%8A%A0%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">八、关于添加对象属性的吐槽</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B9%9D%E3%80%81%E5%85%B3%E4%BA%8E%E8%BE%93%E5%85%A5%E6%A1%86%E9%9D%9E%E7%A9%BA%E7%9A%84%E5%88%A4%E6%96%AD"><span class="toc-text">九、关于输入框非空的判断</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%81%E3%80%81%E5%85%B3%E4%BA%8E%E5%BC%82%E6%AD%A5%E5%87%BD%E6%95%B0%E7%9A%84%E5%90%90%E6%A7%BD"><span class="toc-text">十、关于异步函数的吐槽</span></a></li></ol>
  </div>
</aside>

  
  
  
</div>

    </main>
    
    
<div class="site-footer-wrapper">
<!--
  <div class="footer-top">
    <a class="left" href="/blog/2022/03/03/ES6%E4%B8%AD%E5%85%B3%E4%BA%8E%E5%90%84%E7%9F%A5%E8%AF%86%E7%82%B9%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a class="right" href="/blog/2022/02/17/%E5%B7%A5%E5%85%B7%E6%8E%A8%E8%8D%90-sublime%E7%9A%84%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
-->
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">网站推荐</h5>
          
            <span class="site-footer-item">
              <a href="https://tympanus.net/codrops/" target="_blank">Codrops</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://taobaofed.org/" target="_blank">淘宝前端团队(FED)</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">文档教程</h5>
          
            <span class="site-footer-item">
              <a href="https://es6.ruanyifeng.com/" target="_blank">ES6入门</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://developer.mozilla.org/zh-CN/docs/Web" target="_blank">MDN Web文档</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">兴趣前沿</h5>
          
            <span class="site-footer-item">
              <a href="https://threejs.org/" target="_blank">threejs</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://github.com/justadudewhohacks/face-api.js" target="_blank">face-api</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: xianbin.me@qq.com
      </div>
    
<!--     <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div> -->
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">

  <div class="bottom-bar-left">
<!--
    <a href="/blog/2022/03/03/ES6%E4%B8%AD%E5%85%B3%E4%BA%8E%E5%90%84%E7%9F%A5%E8%AF%86%E7%82%B9%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/2022/02/17/%E5%B7%A5%E5%85%B7%E6%8E%A8%E8%8D%90-sublime%E7%9A%84%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
-->
  </div>

  
  
  
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
    <a id="share-btn-weibo" href="javascript:void(0);" target="_blank">
      <i class="fa fa-weibo"></i>
    </a>
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
</div>
    





    
  </body>
</html>